var vm = new Vue({
  el : '#app',
  extends: CommonTools,
  
  data : function() {
    return {
      leave:{
      },
      teaOptions:{},
      teacher:{},
      show : false,
      leaveId:'',
      
    }
    
  },
  
  filters: {
    /**
     * 状态名称
     */
    stateName(state) {
      for (var i = 0; i < vm.states.length; ++i) {
        if (vm.states[i].value == state) {
          return vm.states[i].label;
        }
      }
      return state;
    },
    
  },
  
 /* computed: {
  readonly: function(){
      return this.activity && (this.activity.state != 'uncommit');
    },
    
    
  },*/
  
  methods: {
    
    /**
     * 重新加载单据
     */
	  reload(){
	      var self = this;
	      var url = "gLeaveController.do?get&id=" 
	        + (this.leaveId?this.leaveId:'');
	      this.get(url).then((data) => {       
	        self.leave = data;
	       if(self.leave != null){
	    	   self.getTea1(self.leave.apCode);
	       }
	        if(self.leave.id != null){
	        	if(self.leave.apResult !="unsubmit"){
	        		self.show = true;
	        	}
	        }
	      }).catch(err => {
	        this.showError(err);
	      });
	    },
    
    /**
     * 保存操作
     */
    onSave() {
        var self = this;
        return self.doSave();
      },

    /**
     * 保存
     */
    doSave(){
      var self = this;
      var url = 'gLeaveController.do?doAdd2';
      self.leave.apName=self.teacher.name;
      self.leave.apCode=self.teacher.teaNum;
      self.leave.apResult="unsubmit";
      self.leave.leName=JSON.parse(sessionStorage.user).name;
      self.leave.leNum=JSON.parse(sessionStorage.user).stuNum;
      self.leave.leClass=JSON.parse(sessionStorage.user).stuClass;
      
      self.leave.leStart = typeof self.leave.leStart == "string" ?self.leave.leStart:self.leave.leStart.format("yyyy-MM-dd hh:mm:ss");
      self.leave.leEnd = typeof self.leave.leEnd == "string" ?self.leave.leEnd:self.leave.leEnd.format("yyyy-MM-dd hh:mm:ss");
        self.post(url, self.leave).then(() => {
          self.showMsg('操作成功！', function() {
            self.goback();
          });
        }).catch(err => {
          self.showError(err);
        });
    },
    
    /**
     * 加载券类型
     */
    getTea() {
      var self = this;
      var url = "gTeacherController.do?datagrid2";
      this.post(url).then((data) => {
        self.teaOptions = data.results;
      }).catch(err => {
        self.showError(err);
      });
    },
    
    getTea1(code) {
    	var self = this;
    	var url = "gTeacherController.do?getByCode&code" + code;
    	this.get(url).then((data) => {
    		self.teacher = data.results;
    	}).catch(err => {
    		self.showError(err);
    	});
    },
    
    goback(){
      history.back();
    },
    /**
     * 删除请假单单
     */
    onDelete(){
      var self = this;
      var url = 'gLeaveController.do?remove&id=' + self.leave.id;
      this.confirm('确认要删除这个请假单吗？', function() {
        self.get(url).then(() => {
          self.showMsg('删除成功！', function() {
            self.goback();
          });
        }).catch(err => {
          self.showMsg('删除失败');
        });
      });
    },
    /**
     * 提交请假单单
     */
    onSubmit(){
    	var self = this;
    	var url = 'gLeaveController.do?submit&id=' + self.leave.id;
    	this.confirm('确认要提交这个请假单吗？', function() {
    		self.get(url).then(() => {
    			self.showMsg('提交成功！', function() {
    				self.goback();
    			});
    		}).catch(err => {
    			self.showMsg('提交失败');
    		});
    	});
    },
    
    
  },
  
  created() {
    this.leaveId = this.getQueryParam('id');
    this.reload();
    this.getTea();
  },
  
});
